/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * torrentGUI.java
 *
 * Created on 05 Mar 12, 21:52:36
 */
package gui;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Timer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JTable;
import javax.swing.filechooser.FileFilter;
import javax.swing.table.DefaultTableModel;
import torrent.Peer;
import torrent.Torrent;

/**
 *
 * @author Owner
 */
public class torrentGUI extends javax.swing.JFrame {

  ArrayList<Torrent> myTorrents = new ArrayList<>();
  LinkedList<Peer> myPeers = new LinkedList<>();

  /**
   * Creates new form torrentGUI
   */
  public torrentGUI() {
    BufferedImage image = null;
    try {
      File iconFile = new File("src/resources/logo.png");
      image = ImageIO.read(iconFile);
    } catch (IOException e) {
    }
    this.setIconImage(image);
    initComponents();
    myTorrents = new ArrayList<>();
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        openTorrent = new javax.swing.JLabel();
        createNewTorrent = new javax.swing.JLabel();
        deleteTorrent = new javax.swing.JLabel();
        nameListScrollPane = new javax.swing.JScrollPane();
        nameTable = new javax.swing.JTable();
        infoTabbedPane = new javax.swing.JTabbedPane();
        peerPane = new javax.swing.JDesktopPane();
        peerScrollPane = new javax.swing.JScrollPane();
        peerTable = new javax.swing.JTable();
        logPane = new javax.swing.JDesktopPane();
        logScrollPane = new javax.swing.JScrollPane();
        textArea = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("myTorrent");

        openTorrent.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/addTorentNormal.png"))); // NOI18N
        openTorrent.setText("New");
        openTorrent.setToolTipText("Open existing torrent");
        openTorrent.setMaximumSize(new java.awt.Dimension(28, 28));
        openTorrent.setMinimumSize(new java.awt.Dimension(28, 28));
        openTorrent.setPreferredSize(new java.awt.Dimension(28, 28));
        openTorrent.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                openTorrentMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                openTorrentMouseExited(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                openTorrentMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                openTorrentMouseReleased(evt);
            }
        });

        createNewTorrent.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/newTorrentNormal.png"))); // NOI18N
        createNewTorrent.setText("New");
        createNewTorrent.setToolTipText("Create new Torrent");
        createNewTorrent.setMaximumSize(new java.awt.Dimension(28, 28));
        createNewTorrent.setMinimumSize(new java.awt.Dimension(28, 28));
        createNewTorrent.setPreferredSize(new java.awt.Dimension(28, 28));
        createNewTorrent.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                createNewTorrentMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                createNewTorrentMouseExited(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                createNewTorrentMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                createNewTorrentMouseReleased(evt);
            }
        });

        deleteTorrent.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/deleteTorentNormal.png"))); // NOI18N
        deleteTorrent.setText("New");
        deleteTorrent.setToolTipText("Delete selected Torrent");
        deleteTorrent.setEnabled(false);
        deleteTorrent.setMaximumSize(new java.awt.Dimension(28, 28));
        deleteTorrent.setMinimumSize(new java.awt.Dimension(28, 28));
        deleteTorrent.setPreferredSize(new java.awt.Dimension(28, 28));
        deleteTorrent.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                deleteTorrentMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                deleteTorrentMouseExited(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                deleteTorrentMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                deleteTorrentMouseReleased(evt);
            }
        });

        nameTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "#", "Filename", "Filesize", "Piece Count", "Piece Size", "Bytes Downloaded", "% Downloaded"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        nameTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        nameListScrollPane.setViewportView(nameTable);
        nameTable.getColumnModel().getColumn(0).setResizable(false);
        nameTable.getColumnModel().getColumn(1).setResizable(false);
        nameTable.getColumnModel().getColumn(2).setResizable(false);
        nameTable.getColumnModel().getColumn(3).setResizable(false);
        nameTable.getColumnModel().getColumn(4).setResizable(false);
        nameTable.getColumnModel().getColumn(5).setResizable(false);

        peerTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "#", "IP", "Port", "Status"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        peerScrollPane.setViewportView(peerTable);
        peerTable.getColumnModel().getColumn(0).setResizable(false);
        peerTable.getColumnModel().getColumn(1).setResizable(false);
        peerTable.getColumnModel().getColumn(2).setResizable(false);
        peerTable.getColumnModel().getColumn(3).setResizable(false);

        peerScrollPane.setBounds(0, 0, 740, 220);
        peerPane.add(peerScrollPane, javax.swing.JLayeredPane.DEFAULT_LAYER);

        infoTabbedPane.addTab("Peer List", peerPane);

        textArea.setColumns(20);
        textArea.setRows(5);
        logScrollPane.setViewportView(textArea);

        logScrollPane.setBounds(0, 0, 740, 220);
        logPane.add(logScrollPane, javax.swing.JLayeredPane.DEFAULT_LAYER);

        infoTabbedPane.addTab("Log", logPane);

        jLabel1.setText("Tracker URL:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(nameListScrollPane, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 742, Short.MAX_VALUE)
                    .addComponent(infoTabbedPane, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 742, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(openTorrent, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(createNewTorrent, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(deleteTorrent, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGap(34, 34, 34))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(createNewTorrent, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(openTorrent, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(deleteTorrent, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(nameListScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addComponent(infoTabbedPane, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(56, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void openTorrentMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_openTorrentMouseEntered
      String imageName = "src/resources/addTorentHovered.png";
      try {
        openTorrent.setIcon(new ImageIcon(ImageIO.read(new File(imageName))));
      } catch (IOException ex) {
        Logger.getLogger(torrentGUI.class.getName()).log(Level.SEVERE, null, ex);
      }
    }//GEN-LAST:event_openTorrentMouseEntered

    private void openTorrentMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_openTorrentMouseExited
      String imageName = "src/resources/addTorentNormal.png";
      try {
        openTorrent.setIcon(new ImageIcon(ImageIO.read(new File(imageName))));
      } catch (IOException ex) {
        Logger.getLogger(torrentGUI.class.getName()).log(Level.SEVERE, null, ex);
      }
    }//GEN-LAST:event_openTorrentMouseExited

    private void createNewTorrentMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_createNewTorrentMouseEntered
      String imageName = "src/resources/newTorrentHovered.png";
      try {
        createNewTorrent.setIcon(new ImageIcon(ImageIO.read(new File(imageName))));
      } catch (IOException ex) {
        Logger.getLogger(torrentGUI.class.getName()).log(Level.SEVERE, null, ex);
      }
    }//GEN-LAST:event_createNewTorrentMouseEntered

    private void createNewTorrentMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_createNewTorrentMouseExited
      String imageName = "src/resources/newTorrentNormal.png";
      try {
        createNewTorrent.setIcon(new ImageIcon(ImageIO.read(new File(imageName))));
      } catch (IOException ex) {
        Logger.getLogger(torrentGUI.class.getName()).log(Level.SEVERE, null, ex);
      }
    }//GEN-LAST:event_createNewTorrentMouseExited

    private void deleteTorrentMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_deleteTorrentMouseEntered
      String imageName = "src/resources/deleteTorentHovered.png";
      try {
        deleteTorrent.setIcon(new ImageIcon(ImageIO.read(new File(imageName))));
      } catch (IOException ex) {
        Logger.getLogger(torrentGUI.class.getName()).log(Level.SEVERE, null, ex);
      }
    }//GEN-LAST:event_deleteTorrentMouseEntered

    private void deleteTorrentMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_deleteTorrentMouseExited
      String imageName = "src/resources/deleteTorentNormal.png";
      try {
        deleteTorrent.setIcon(new ImageIcon(ImageIO.read(new File(imageName))));
      } catch (IOException ex) {
        Logger.getLogger(torrentGUI.class.getName()).log(Level.SEVERE, null, ex);
      }
    }//GEN-LAST:event_deleteTorrentMouseExited

    private void deleteTorrentMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_deleteTorrentMousePressed
      String imageName = "src/resources/deleteTorentClicked.png";
      try {
        deleteTorrent.setIcon(new ImageIcon(ImageIO.read(new File(imageName))));
      } catch (IOException ex) {
        Logger.getLogger(torrentGUI.class.getName()).log(Level.SEVERE, null, ex);
      }
    }//GEN-LAST:event_deleteTorrentMousePressed

    private void createNewTorrentMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_createNewTorrentMousePressed
      String imageName = "src/resources/newTorrentClicked.png";
      try {
        createNewTorrent.setIcon(new ImageIcon(ImageIO.read(new File(imageName))));
      } catch (IOException ex) {
        Logger.getLogger(torrentGUI.class.getName()).log(Level.SEVERE, null, ex);
      }
    }//GEN-LAST:event_createNewTorrentMousePressed

    private void openTorrentMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_openTorrentMousePressed
      String imageName = "src/resources/addTorentClicked.png";
      try {
        openTorrent.setIcon(new ImageIcon(ImageIO.read(new File(imageName))));
      } catch (IOException ex) {
        Logger.getLogger(torrentGUI.class.getName()).log(Level.SEVERE, null, ex);
      }
    }//GEN-LAST:event_openTorrentMousePressed

    private void openTorrentMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_openTorrentMouseReleased
      String imageName = "src/resources/addTorentNormal.png";
      try {
        openTorrent.setIcon(new ImageIcon(ImageIO.read(new File(imageName))));
      } catch (IOException ex) {
        Logger.getLogger(torrentGUI.class.getName()).log(Level.SEVERE, null, ex);
      }
      JFileChooser fc = new JFileChooser("./");
      fc.setFileFilter(new FileFilter() {

        @Override
        public boolean accept(File f) {
          return f.isDirectory() || f.getName().toLowerCase().endsWith(".torrent");
        }

        @Override
        public String getDescription() {
          return ".torrent files";
        }
      });
      int retVal = fc.showOpenDialog(fc);
      if (retVal == JFileChooser.APPROVE_OPTION) {
        myTorrents.add(new Torrent());
        myTorrents.get(myTorrents.size() - 1).readFileTorrent(fc.getSelectedFile());
        ((DefaultTableModel) nameTable.getModel()).addRow(new Object[]{nameTable.getRowCount() + 1, myTorrents.get(myTorrents.size() - 1).fileName, (myTorrents.get(myTorrents.size() - 1).fileLength / 1000) + " KB", myTorrents.get(myTorrents.size() - 1).pieceCount, (myTorrents.get(myTorrents.size() - 1).pieceLength / 1000) + " KB", 0, 0});
        nameTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        myPeers.add(new Peer(fc.getSelectedFile()));
        Peer p = myPeers.getLast();
        p.sendGetRequest(p.url, p.query);
        textArea.setText(textArea.getText() + p.response + "\n");
//        Timer t = new Timer();
//        t.schedule(myPeers.removeLast(), 5000, 50000);
      }
    }//GEN-LAST:event_openTorrentMouseReleased

    private void createNewTorrentMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_createNewTorrentMouseReleased
      String imageName = "src/resources/newTorrentNormal.png";
      try {
        createNewTorrent.setIcon(new ImageIcon(ImageIO.read(new File(imageName))));
        createTorrent ct = new createTorrent(this, true);
        ct.setLocationRelativeTo(this);
        ct.setVisible(true);
      } catch (IOException ex) {
        Logger.getLogger(torrentGUI.class.getName()).log(Level.SEVERE, null, ex);
      }
    }//GEN-LAST:event_createNewTorrentMouseReleased

    private void deleteTorrentMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_deleteTorrentMouseReleased
      String imageName = "src/resources/deleteTorentNormal.png";
      try {
        deleteTorrent.setIcon(new ImageIcon(ImageIO.read(new File(imageName))));
      } catch (IOException ex) {
        Logger.getLogger(torrentGUI.class.getName()).log(Level.SEVERE, null, ex);
      }
    }//GEN-LAST:event_deleteTorrentMouseReleased

  /**
   * @param args the command line arguments
   */
  public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

      @Override
      public void run() {
        new torrentGUI().setVisible(true);
      }
    });
  }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel createNewTorrent;
    private javax.swing.JLabel deleteTorrent;
    private javax.swing.JTabbedPane infoTabbedPane;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JDesktopPane logPane;
    private javax.swing.JScrollPane logScrollPane;
    private javax.swing.JScrollPane nameListScrollPane;
    private javax.swing.JTable nameTable;
    private javax.swing.JLabel openTorrent;
    private javax.swing.JDesktopPane peerPane;
    private javax.swing.JScrollPane peerScrollPane;
    private javax.swing.JTable peerTable;
    private javax.swing.JTextArea textArea;
    // End of variables declaration//GEN-END:variables
  public DefaultTableModel peerTableModel;
}
